from django.shortcuts import render,get_object_or_404
from django.http import HttpResponse
from django.template import loader

from .models import User,Udetail
# Create your views here.

def setCookie(request, usr, name):
    response = render(request, "home/index.html", {'login':name})
    response.set_cookie('usr', usr)
    return response

def index(request):
    usr = request.COOKIES.get('usr')
    if usr:
        res = User.objects.get(sno=usr)
        name = res.udetail_set.get(sno=usr).name
        return render(request, 'home/index.html', {'login':name})
    elif request.method == 'POST':
        usr = request.POST['usr']
        psw = request.POST['psw']
        try:
            res = User.objects.get(sno=usr, psw=psw)
            name = res.udetail_set.get(sno=usr).name
            return setCookie(request, usr, name)
        except:
            return render(request, "home/index.html", {'error':True})
    else:
        return render(request, "home/index.html")

def login(request):
    return render(request, "home/index.html")
    
def log_index(request):
    return render(request,"home/log_index.html")